import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * database and server files directory configuration
 *
 */
public class Configuration extends JDialog {

	private static final long serialVersionUID = 1L;
	/** window panel */
	private JPanel jContentPane = null;
	/** directory of the host */
	private JTextField hostDir = null;
	/** database - username */
	private JTextField user = null;
	/** database - password */
	private JTextField pass = null;
	/** host directory label */
	private JLabel hostLabel = null;
	/** username label */
	private JLabel userLabel = null;
	/** password label */
	private JLabel passLabel = null;
	/** title label */
	private JLabel cfgTitle = null;
	/** ok button - save changes */
	private JButton ok = null;
	/** set textfields for default */
	private JButton def = null;
	/** goes back to login window */
	private JButton back = null;
	/** flag indicating if changes were made and were not saved yet */
	private Boolean changes=false;  //  @jve:decl-index=0:
	/** books files directory label */
	private JLabel filesLabel = null;
	/** books files directory */
	private JTextField filesDir = null;
	

	/**
	 * @param owner
	 * @return 
	 * @return 
	 */
	public Configuration(Frame owner) 
	{
		super(owner);
		initialize();
		//return owner;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 328);
		this.setContentPane(getJContentPane());
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			filesLabel = new JLabel();
			filesLabel.setBounds(new Rectangle(16, 196, 76, 27));
			filesLabel.setText("Files Dir:");
			cfgTitle = new JLabel();
			cfgTitle.setBounds(new Rectangle(68, 8, 246, 47));
			cfgTitle.setText("JDBC Config");
			cfgTitle.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,40));
			passLabel = new JLabel();
			passLabel.setBounds(new Rectangle(16, 155, 76, 28));
			passLabel.setText("Password:");
			userLabel = new JLabel();
			userLabel.setBounds(new Rectangle(16, 110, 75, 27));
			userLabel.setText("Username:");
			hostLabel = new JLabel();
			hostLabel.setBounds(new Rectangle(16, 68, 76, 27));
			hostLabel.setText("Host Dir:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBack(), null);
			jContentPane.add(getHostDir(), null);
			jContentPane.add(getUser(), null);
			jContentPane.add(getPass(), null);
			jContentPane.add(hostLabel, null);
			jContentPane.add(userLabel, null);
			jContentPane.add(passLabel, null);
			jContentPane.add(cfgTitle, null);
			jContentPane.add(getOk(), null);
			jContentPane.add(getDef(), null);
			jContentPane.add(filesLabel, null);
			jContentPane.add(getFilesDir(), null);
			
		}
		return jContentPane;
	}

	/**
	 * This method initializes hostDir and sets listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getHostDir() {
		if (hostDir == null) {
			hostDir = new JFormattedTextField();
			hostDir.setBounds(new Rectangle(97, 70, 263, 26));
			hostDir.setText(Main.sets[0]);
			hostDir.addKeyListener(new java.awt.event.KeyListener() {
				public void keyReleased(java.awt.event.KeyEvent e) 
				{
					changes=true;
				}
				public void keyTyped(java.awt.event.KeyEvent e) 
				{
					Character c=e.getKeyChar();
					if(c.charValue()=='*')
					{
						e.consume();
						Toolkit.getDefaultToolkit().beep();
					}
				}
				public void keyPressed(java.awt.event.KeyEvent e) {
				}
			});
		}
		return hostDir;
	}

	/**
	 * This method initializes user	and sets listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUser() {
		if (user == null) {
			user = new JTextField();
			user.setBounds(new Rectangle(97, 112, 263, 26));
			user.setText(Main.sets[1]);
			user.addKeyListener(new java.awt.event.KeyListener() {
				public void keyReleased(java.awt.event.KeyEvent e) {
						changes=true;
				}
				public void keyTyped(java.awt.event.KeyEvent e) 
				{
					Character c=e.getKeyChar();
					if(c.charValue()=='*')
					{
						e.consume();
						Toolkit.getDefaultToolkit().beep();
					}
				}
				public void keyPressed(java.awt.event.KeyEvent e) {
				}
			});
		}
		return user;
	}

	/**
	 * This method initializes pass	and sets listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPass() {
		if (pass == null) {
			pass = new JTextField();
			pass.setBounds(new Rectangle(94, 157, 263, 26));
			pass.setText(Main.sets[2]);
			pass.addKeyListener(new java.awt.event.KeyListener() {
				public void keyReleased(java.awt.event.KeyEvent e) {
						changes=true;
				}
				public void keyTyped(java.awt.event.KeyEvent e) {
					Character c=e.getKeyChar();
					if(c.charValue()=='*')
					{
						e.consume();
						Toolkit.getDefaultToolkit().beep();
					}
				}
				public void keyPressed(java.awt.event.KeyEvent e) {
				}
			});
		}
		return pass;
	}

	/**
	 * This method initializes ok	
	 * 	
	 * @return javax.swing.JButton and sets listener
	 */
	private JButton getOk() {
		if (ok == null) {
			ok = new JButton("Set Configuration");
			ok.setBounds(new Rectangle(6, 245, 136, 33));
			ok.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Main.sets[0]=hostDir.getText();
					Main.sets[1]=user.getText();
					Main.sets[2]=pass.getText();
					Main.sets[3]=filesDir.getText();
					JOptionPane.showMessageDialog(null,"JDBC Configuration Saved");
					changes=false;
				}
			});
		}
		return ok;
	}

	/**
	 * This method initializes def and sets listener	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDef() {
		if (def == null) {
			def = new JButton("Set Default");
			def.setBounds(new Rectangle(150, 245, 136, 33));
			def.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					pass.setText("1234");
					user.setText("root");
					hostDir.setText("jdbc:mysql://localhost/goodreading");
					filesDir.setText("d:\\Files");
					changes=true;
				}
			});
		}
		return def;
	}

	/**
	 * This method initializes back	and sets listener
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBack() {
		if (back == null) {
			back = new JButton("Close");
			back.setBounds(new Rectangle(294, 245, 78, 30));
			back.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(changes)
					{
						if(JOptionPane.showConfirmDialog(null, "Do you want to save changes?","Save", 0) == 0)
						{
							Main.sets[0]=hostDir.getText();
							Main.sets[1]=user.getText();
							Main.sets[2]=pass.getText();
							Main.sets[3]=filesDir.getText();
							JOptionPane.showMessageDialog(null,"JDBC Configuration Saved");
						}
					}
					
					Main.log.panel.setEnabled(true);
					dispose();
				}
			});
		}
		return back;
	}

	/**
	 * This method initializes filesDir	and sets listener
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFilesDir() {
		if (filesDir == null) {
			filesDir = new JTextField();
			filesDir.setText(Main.sets[3]);
			filesDir.setBounds(new Rectangle(94, 197, 258, 23));
			filesDir.addKeyListener(new java.awt.event.KeyListener() {
				public void keyReleased(java.awt.event.KeyEvent e) {
						changes=true;
				}
				public void keyTyped(java.awt.event.KeyEvent e) {
					Character c=e.getKeyChar();
					if(c.charValue()=='*')
					{
						e.consume();
						Toolkit.getDefaultToolkit().beep();
					}
				}
				public void keyPressed(java.awt.event.KeyEvent e) {
				}
			});
		}
		return filesDir;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
